﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using Session;


public partial class Admin_Physprops : System.Web.UI.Page
{
    Service objService = new Service();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];

            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }

            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillPhysicalPropertiesDetails(Convert.ToInt32(id));
                txtPhysicalProId.Text = string.Empty;
                trDgPhysicalProperties.Visible = false;
                trTextBoxPhysicalProperties.Visible = true;
            }
            fillDataGrid();
        }
    }
    private void fillDataGrid()
    {
        string ExpId = (string)Session["ExperimentId"];
        if (ExpId != null)
        {
            Int32 PhyspropsId = objService.GetExperimentFKValues("physprops_fk", ExpId);
            if (PhyspropsId.ToString() == null)
            {
                lblMessage.Text = "You haven't entered any data for the Physical properties to this experiment";
            }
            else
            {
                if (objService.GetPhysicalProperties(PhyspropsId.ToString()).Tables[0].Rows.Count > 0)
                {
                    dgPhysicalprops.DataSource = objService.GetPhysicalProperties(PhyspropsId.ToString());
                    dgPhysicalprops.DataBind();
                    trDgPhysicalProperties.Visible = true;
                    trTextBoxPhysicalProperties.Visible = false;
                }
                else
                {
                    trDgPhysicalProperties.Visible = false;
                    trTextBoxPhysicalProperties.Visible = true;
                }
                
            }
        }
        else
        {
            trDgPhysicalProperties.Visible = false;
            trTextBoxPhysicalProperties.Visible = true;
            lblMessage.Text = "You haven't entered any data for the Physical properties to this experiment";
        }
    }

   

    private double  findPrediDiameter(double i, double j, double k)
    {
       double  r, l, d;      
       l = (i * j * k * 0.75) / Math.PI;
       r  = Math.Pow(l ,(1 / 3.0));
       d = 2 * r;
       return d;
    }

    private double findPrediDiameter(double i, double j)
    {
        double r, l, d;
        l = (i * j * 0.75) / Math.PI;
        r = Math.Pow(l, (1 / 3.0));
        d = 2 * r;
        return d;
    }

    private void clearTextBoxes()
    {
        txtPhysicalProId.Text = string.Empty;
        txtDiamterMax.Text = string.Empty;
        txtDiamterMin.Text = string.Empty;
        txtLength3.Text = string.Empty;
        txtLength2.Text = string.Empty;
        txtLength1.Text = string.Empty;
        txtMoistureMax.Text = string.Empty;
        txtMoistureMin.Text = string.Empty;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        
    }


    private void fillPhysicalPropertiesDetails(Int32 id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("physprops", id);
        txtDiamterMin .Text = ds.Tables[0].Rows[0]["diam_min"].ToString();
        txtDiamterMax.Text = ds.Tables[0].Rows[0]["diam_max"].ToString();
        txtLength1.Text = ds.Tables[0].Rows[0]["length1"].ToString();
        txtLength2.Text = ds.Tables[0].Rows[0]["length2"].ToString();
        txtLength3.Text = ds.Tables[0].Rows[0]["length3"].ToString();
        txtMoistureMin.Text = ds.Tables[0].Rows[0]["moisture_min"].ToString();
        txtMoistureMax.Text = ds.Tables[0].Rows[0]["moisture_max"].ToString();
        txtPhysicalProId.Text = ds.Tables[0].Rows[0]["id"].ToString();
    }

    protected void dgPhysicalprops_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgPhysicalprops.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillPhysicalPropertiesDetails(id);
        trDgPhysicalProperties.Visible = false;
        trTextBoxPhysicalProperties.Visible = true;
        btnPrevious.Visible = false;

    }
    protected void dgPhysicalprops_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgPhysicalprops.PageIndex = e.NewPageIndex;
        fillDataGrid();
    }
    protected void btnSave_Click1(object sender, ImageClickEventArgs e)
    {
        string RefeId = (string)Session["ReferenceId"];

        if (txtPhysicalProId.Text != string.Empty)
        {
            
            if (txtDiamterMin.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("physprops", "diam_min", "null", txtPhysicalProId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("physprops", "diam_min", txtDiamterMin.Text, txtPhysicalProId.Text);
            }

            
            if (txtDiamterMax.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("physprops", "diam_max", "null" , txtPhysicalProId.Text);
                
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("physprops", "diam_max", txtDiamterMax.Text, txtPhysicalProId.Text);
            }

            if (txtLength1.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("physprops", "length1", "null", txtPhysicalProId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("physprops", "length1", txtLength1.Text, txtPhysicalProId.Text);
            }

            if (txtLength2.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("physprops", "length2", "null", txtPhysicalProId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("physprops", "length2", txtLength2.Text, txtPhysicalProId.Text);
            }
            if (txtLength3.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("physprops", "length3", "null", txtPhysicalProId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("physprops", "length3", txtLength3.Text, txtPhysicalProId.Text);
            }
            if (txtMoistureMin.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("physprops", "moisture_min", "null", txtPhysicalProId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("physprops", "moisture_min", txtMoistureMin.Text, txtPhysicalProId.Text);
            }
            if (txtMoistureMax.Text != string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("physprops", "moisture_max", txtMoistureMax.Text, txtPhysicalProId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("physprops", "moisture_max", "null", txtPhysicalProId.Text);
            }

            if (txtLength1.Text != string.Empty & txtLength2.Text != string.Empty & txtLength3.Text != string.Empty)
            {
                double prediDia = findPrediDiameter(Convert.ToDouble(txtLength1.Text), Convert.ToDouble(txtLength2.Text), Convert.ToDouble(txtLength3.Text));
                lblMessage.Text = objService.UpdateTable("physprops", "pred_diam", prediDia.ToString(), txtPhysicalProId.Text);
            }
            else if (txtLength1.Text != string.Empty & txtLength2.Text != string.Empty)// ||  || )
            {
                double prediDia = findPrediDiameter(Convert.ToDouble(txtLength1.Text), Convert.ToDouble(txtLength2.Text));
                lblMessage.Text = objService.UpdateTable("physprops", "pred_diam", prediDia.ToString(), txtPhysicalProId.Text);
            }
            else if (txtLength1.Text != string.Empty & txtLength3.Text != string.Empty)
            {
                double prediDia = findPrediDiameter(Convert.ToDouble(txtLength1.Text), Convert.ToDouble(txtLength3.Text));
                lblMessage.Text = objService.UpdateTable("physprops", "pred_diam", prediDia.ToString(), txtPhysicalProId.Text);
            }
            else if (txtLength2.Text != string.Empty & txtLength3.Text != string.Empty)
            {
                double prediDia = findPrediDiameter(Convert.ToDouble(txtLength2.Text), Convert.ToDouble(txtLength3.Text));
                lblMessage.Text = objService.UpdateTable("physprops", "pred_diam", prediDia.ToString(), txtPhysicalProId.Text);
            }

            else if (txtLength1.Text == string.Empty & txtLength2.Text == string.Empty & txtLength3.Text != string.Empty)
            {
                double prediDia = Convert.ToDouble(txtLength3.Text);
                lblMessage.Text = objService.UpdateTable("physprops", "pred_diam", prediDia.ToString(), txtPhysicalProId.Text);
            }
            else if (txtLength1.Text == string.Empty & txtLength2.Text != string.Empty & txtLength3.Text == string.Empty)
            {
                double prediDia = Convert.ToDouble(txtLength2.Text);
                lblMessage.Text = objService.UpdateTable("physprops", "pred_diam", prediDia.ToString(), txtPhysicalProId.Text);
            }
            else if (txtLength1.Text != string.Empty & txtLength2.Text == string.Empty & txtLength3.Text == string.Empty)
            {
                double prediDia = Convert.ToDouble(txtLength1.Text);
                lblMessage.Text = objService.UpdateTable("physprops", "pred_diam", prediDia.ToString(), txtPhysicalProId.Text);

            }

            //else if (txtLength1.Text == string.Empty & txtLength2.Text == string.Empty & txtLength3.Text == string.Empty)
            //{
               
            //    lblMessage.Text = objService.SavePhysicalproperties(txtDiamterMin.Text, txtDiamterMax.Text, txtLength1.Text, txtLength2.Text, txtLength3.Text, "", txtMoistureMin.Text, txtMoistureMax.Text);
            //}

            else if(txtLength1.Text==string.Empty & txtLength2.Text==string.Empty & txtLength3.Text==string.Empty )
            {
                lblMessage.Text = objService.UpdateTable("physprops", "pred_diam", "null", txtPhysicalProId.Text);
            }
            fillDataGrid();

            clearTextBoxes();
        }
        else
        {

            if (txtLength1.Text != string.Empty & txtLength2.Text != string.Empty & txtLength3.Text != string.Empty)
            {
                double prediDia = findPrediDiameter(Convert.ToDouble(txtLength1.Text), Convert.ToDouble(txtLength2.Text), Convert.ToDouble(txtLength3.Text));
                lblMessage.Text = objService.SavePhysicalproperties(txtDiamterMin.Text, txtDiamterMax.Text, txtLength1.Text, txtLength2.Text, txtLength3.Text, prediDia.ToString(), txtMoistureMin.Text, txtMoistureMax.Text);
            }
            else if (txtLength1.Text != string.Empty & txtLength2.Text != string.Empty & txtLength3.Text ==string.Empty )
            {
                double prediDia = findPrediDiameter(Convert.ToDouble(txtLength1.Text), Convert.ToDouble(txtLength2.Text));
                lblMessage.Text = objService.SavePhysicalproperties(txtDiamterMin.Text, txtDiamterMax.Text, txtLength1.Text, txtLength2.Text, txtLength3.Text, prediDia.ToString(), txtMoistureMin.Text, txtMoistureMax.Text);
            }


            else if (txtLength1.Text != string.Empty & txtLength3.Text != string.Empty & txtLength2.Text == string.Empty)
            {
                double prediDia = findPrediDiameter(Convert.ToDouble(txtLength1.Text), Convert.ToDouble(txtLength3.Text));
                lblMessage.Text = objService.SavePhysicalproperties(txtDiamterMin.Text, txtDiamterMax.Text, txtLength1.Text, txtLength2.Text, txtLength3.Text, prediDia.ToString(), txtMoistureMin.Text, txtMoistureMax.Text);
            }
            else if (txtLength2.Text != string.Empty & txtLength3.Text != string.Empty & txtLength1.Text == string.Empty)
            {
                double prediDia = findPrediDiameter(Convert.ToDouble(txtLength2.Text), Convert.ToDouble(txtLength3.Text));
                lblMessage.Text = objService.SavePhysicalproperties(txtDiamterMin.Text, txtDiamterMax.Text, txtLength1.Text, txtLength2.Text, txtLength3.Text, prediDia.ToString(), txtMoistureMin.Text, txtMoistureMax.Text);
            }


            else if (txtLength1.Text == string.Empty & txtLength2.Text == string.Empty & txtLength3.Text != string.Empty)
            {
                double prediDia = Convert.ToDouble(txtLength3.Text);
                lblMessage.Text = objService.SavePhysicalproperties(txtDiamterMin.Text, txtDiamterMax.Text, txtLength1.Text, txtLength2.Text, txtLength3.Text, prediDia.ToString(), txtMoistureMin.Text, txtMoistureMax.Text);
            }

            else if (txtLength1.Text == string.Empty & txtLength2.Text != string.Empty & txtLength3.Text == string.Empty)
            {
                double prediDia = Convert.ToDouble(txtLength2.Text);
                lblMessage.Text = objService.SavePhysicalproperties(txtDiamterMin.Text, txtDiamterMax.Text, txtLength1.Text, txtLength2.Text, txtLength3.Text, prediDia.ToString(), txtMoistureMin.Text, txtMoistureMax.Text);
            }
            else if (txtLength1.Text != string.Empty & txtLength2.Text == string.Empty & txtLength3.Text == string.Empty)
            {
                double prediDia = Convert.ToDouble(txtLength1.Text);
                lblMessage.Text = objService.SavePhysicalproperties(txtDiamterMin.Text, txtDiamterMax.Text, txtLength1.Text, txtLength2.Text, txtLength3.Text, prediDia.ToString(), txtMoistureMin.Text, txtMoistureMax.Text);
            }

            else if (txtLength1.Text == string.Empty & txtLength2.Text == string.Empty & txtLength3.Text == string.Empty)
            {
                //double prediDia = Convert.ToDouble(txtLength1.Text);
                lblMessage.Text = objService.SavePhysicalproperties(txtDiamterMin.Text, txtDiamterMax.Text, txtLength1.Text, txtLength2.Text, txtLength3.Text, "", txtMoistureMin.Text, txtMoistureMax.Text);
            }

            string PhysicalPropertiesId = objService.GetLastInsertedId("ref_fk", RefeId, "physprops");
               

            objService.UpdateTable("physprops", "ref_fk", RefeId, PhysicalPropertiesId);

             clearTextBoxes();

            
            if ((string)Session["ExperimentId"] != null)
            {
                string ExpId = (string)Session["ExperimentId"];
                if (objService.UpdateExperiment("physprops_fk", PhysicalPropertiesId, ExpId) != "Experiment Successfully Updated")
                {
                    objService.DeleteRecord("physprops", Convert.ToInt32(PhysicalPropertiesId));
                    lblMessage.Text = "Admin - You can't enter the multiple Physical Properties records for one experiment";
                    fillDataGrid();
                }
                else
                {
                    lblMessage.Text = "Physical Properties Saved and " + " " + objService.UpdateExperiment("physprops_fk", PhysicalPropertiesId, ExpId);
                    fillDataGrid();
                }

                lblExpdetails.Text = "Your Experiment Id is - " + ExpId;

            }
            else
            {
                lblMessage.Text = "Physical Properties Saved and  " + " " + objService.SaveExperiment(null, null, null, null, null, PhysicalPropertiesId, null, null, null, null, null, RefeId,null);
                string ExperimentId = objService.GetExperimentId("physprops_fk", PhysicalPropertiesId);
                Session["ExperimentId"] = ExperimentId;

                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;


                fillDataGrid();
            }
        }
    }

    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PrePhysicalProperties.aspx");
    }
}
